﻿using System;
using System.IO;
using UnityEngine;

namespace Database
{
    public class BaseOrm
    {
        public int uniqueId;

        public static char commaChar = ',';
        public static char uLineChar = '_';

        public virtual void LoadBinary(BinaryReader br)
        {
            throw new NotImplementedException("LoadBinary");
        }

        public virtual void LoadRelevance()
        {
        }


        public virtual Color ConvertColor32(string value, char splitChar)
        {
            var ary = SplitChar(value, splitChar);
            Color32 v = new Color32();
            v.r = Convert.ToByte(ary[0]);
            v.g = Convert.ToByte(ary[1]);
            v.b = Convert.ToByte(ary[2]);
            v.a = Convert.ToByte(ary[2]);
            return v;
        }

        public virtual Color ConvertColor(string value, char splitChar)
        {
            if (string.IsNullOrEmpty(value))
            {
                return Color.white;
            }

            var ary = SplitChar(value, splitChar);
            Color v = new Color();

            v.r = Convert.ToSingle(ary[0]) / 255f;
            v.g = Convert.ToSingle(ary[1]) / 255f;
            v.b = Convert.ToSingle(ary[2]) / 255f;
            v.a = Convert.ToSingle(ary[2]) / 255f;
            return v;
        }

        public virtual Vector3 ConvertVector3(string value, char splitChar)
        {
            if (string.IsNullOrEmpty(value))
            {
                return Vector3.zero;
            }

            var ary = SplitChar(value, splitChar);
            Vector3 v = new Vector3();
            v.x = Convert.ToSingle(ary[0]);
            v.y = Convert.ToSingle(ary[1]);
            v.z = Convert.ToSingle(ary[2]);
            return v;
        }

        public virtual Vector2 ConvertVector2(string value, char splitChar)
        {
            var ary = SplitChar(value, splitChar);
            Vector2 v = new Vector2();
            v.x = Convert.ToSingle(ary[0]);
            v.y = Convert.ToSingle(ary[1]);
            return v;
        }

        public virtual string[] SplitChar(string value, char splitChar, bool trimStartEnd = true)
        {
            string[] charAry = null;

            if (trimStartEnd)
            {
                value = value.Trim(splitChar);
            }

            charAry = value.Split(splitChar);

            return charAry;
        }
    }
}